package bookstudytest.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: Lee
 * @Date: 2019/3/8 0008 14:02
 * @Description:
 */
public class HiProxy implements MethodInterceptor {
    /**
     * 绑定实际对象和代理对象
     *
     * @param cls
     * @return
     */
    public Object bind(Class cls) {
        // 增强方法类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(cls);
        enhancer.setCallback(this);
        return enhancer.create();
    }


    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("say hi before");
        Object result = methodProxy.invokeSuper(o, objects);


        System.out.println("say hi after");
        return result;
    }
}
